package com.biwin.core.main.handler;

import com.biwin.common.app.enums.BwBasicResponseEnum;
import com.biwin.common.app.vo.BwResponse;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * 全局的 Controller 返回增强,无需在 Controller 的函数中单独封装响应体.
 *
 * @author biwin
 * @since 0.0.1
 */
@RestControllerAdvice(basePackages = {"com.biwin"})
public class ResponseControllerAdvice implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> aClass) {
        // 如果接口返回的类型本身就是ResultVO那就没有必要进行额外的操作，返回false
        return !returnType.getGenericParameterType().equals(BwResponse.class);
    }

    @Override
    public Object beforeBodyWrite(Object data, MethodParameter returnType,
                                  MediaType mediaType,
                                  Class<? extends HttpMessageConverter<?>> aClass,
                                  ServerHttpRequest serverHttpRequest,
                                  ServerHttpResponse serverHttpResponse) {
        // String类型不能直接包装，所以要进行些特别的处理
        if (returnType.getGenericParameterType().equals(String.class) ) {
            ObjectMapper objectMapper = new ObjectMapper();
            try {
                // 将数据包装在ResultVO里后，再转换为json字符串响应给前端
                return objectMapper.writeValueAsString(BwResponse.response(BwBasicResponseEnum.REQUEST_SUCCESS, data));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        // 将原本的数据包装在ResultVO里
        return BwResponse.SUCCESS(data);
    }

}
